package com.huarui.quotation.action;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.apache.commons.lang.StringUtils;
import org.ewaf.framework.action.BaseAction;
import com.huarui.params.Params;
import com.huarui.quotation.service.QuotationSearchService;
import com.huarui.util.FuncUtil;

public class QuotationFirstPageSearchAction extends BaseAction{
	
	public final static String MESS = "mess";
	
	private static final long serialVersionUID = 1389475373041439907L;
	
	private Map<String, Object> mesMap;
	
	private QuotationSearchService quotationSearchService;
	
	private List<Map<String, Object>> quotationList;
	
	private Integer totalProperty;
		
	private Integer startPage;
	
	private Integer pageCount;
	
	private Integer source;
	
	private String code;
	
	private String name;
	
	private String brand;
	
	private String carModel;

	public String getCarModel() {
		return carModel;
	}

	public void setCarModel(String carModel) {
		this.carModel = carModel;
	}

	public Integer getSource() {
		return source;
	}

	public void setSource(Integer source) {
		this.source = source;
	}

	public String getCode() {
		return code;
	}

	public void setCode(String code) {
		this.code = code;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getBrand() {
		return brand;
	}

	public void setBrand(String brand) {
		this.brand = brand;
	}

	public List<Map<String, Object>> getQuotationList() {
		return quotationList;
	}

	public void setQuotationList(List<Map<String, Object>> quotationList) {
		this.quotationList = quotationList;
	}

	public Integer getTotalProperty() {
		return totalProperty;
	}

	public void setTotalProperty(Integer totalProperty) {
		this.totalProperty = totalProperty;
	}

	public Integer getStartPage() {
		return startPage;
	}

	public void setStartPage(Integer startPage) {
		this.startPage = startPage;
	}

	public Integer getPageCount() {
		return pageCount;
	}

	public void setPageCount(Integer pageCount) {
		this.pageCount = pageCount;
	}

	
	public Map<String, Object> getMesMap() {
		return mesMap;
	}

	public void setMesMap(Map<String, Object> mesMap) {
		this.mesMap = mesMap;
	}

	public void setQuotationSearchService(
			QuotationSearchService quotationSearchService) {
		this.quotationSearchService = quotationSearchService;
	}

	
	/**
	 * 首页搜索
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public String searchQuotationOnFirstPage() {
		Map<String, Object> paramsMap = new HashMap<String, Object>();
		
		if(StringUtils.isNotBlank(brand)) paramsMap.put("brand", brand);
		if(StringUtils.isNotBlank(name)) paramsMap.put("name", name);
		if(source != null) paramsMap.put("source", source);
		if(StringUtils.isNotBlank(code)) paramsMap.put("code", code);
		if(StringUtils.isNotBlank(carModel)) paramsMap.put("carModel", carModel);
		
		paramsMap.put("start", (startPage-1) * Params.PAGE_SIZE);
		paramsMap.put("pageSize", Params.PAGE_SIZE);
		
		String ipAddress = super.httpServletRequest.getRemoteAddr();
		Long ip = FuncUtil.ip2Long(ipAddress);
		paramsMap.put("ip", ip);
		
		mesMap = this.quotationSearchService.searchQuotationOnFirstPage(paramsMap);
		quotationList = (List<Map<String, Object>>)mesMap.get("root");
		totalProperty = (Integer)mesMap.get("totalProperty");
		pageCount = (totalProperty > Params.PAGE_SIZE && totalProperty % Params.PAGE_SIZE == 0) ? 
				totalProperty / Params.PAGE_SIZE : totalProperty / Params.PAGE_SIZE + 1;
				
		return MESS;
	}
}
